# -* -coding: utf-8 -*-
import random
import requests
import win32api
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time,json,threading
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from datetime import datetime
# 返回一个随机的请求头 headers
def get_user_agent():
    # 各种PC端
    user_agent_two = [
        "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0",
        # Opera
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60",
        "Opera/8.0 (Windows NT 5.1; U; en)",
        "Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.50",
        # Firefox
        "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0",
        "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
        # Safari
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
        # chrome
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gnavigator.webdriverecko) Chrome/39.0.2171.71 Safari/537.36",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
        "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16",
        # 360
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
        # 淘宝浏览器
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11",
        # 猎豹浏览器
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
        "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)",
        # QQ浏览器
        "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)",
        # sogou浏览器
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0",
        # maxthon浏览器
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.3.4000 Chrome/30.0.1599.101 Safari/537.36",
        # UC浏览器
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 UBrowser/4.0.3214.0 Safari/537.36",
    ]
    # 各种移动端
    user_agent_three = [
        # IPhone
        "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
        # IPod
        "Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
        # IPAD
        "Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5",
        "Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
        # Android
        "Mozilla/5.0 (Linux; U; Android 2.2.1; zh-cn; HTC_Wildfire_A3333 Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
        "Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
        # QQ浏览器 Android版本
        "MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
        # Android Opera Mobile
        "Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
        # Android Pad Moto Xoom
        "Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13",
        # BlackBerry
        "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+",
        # WebOS HP Touchpad
        "Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0",
        # Nokia N97
        "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
        # Windows Phone Mango
        "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)",
        # UC浏览器
        "UCWEB7.0.2.37/28/999",
        "NOKIA5700/ UCWEB7.0.2.37/28/999",
        # UCOpenwave
        "Openwave/ UCWEB7.0.2.37/28/999",
        # UC Opera
        "Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/999"
    ]
    # 一部分 PC端的
    user_agent_one = [
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
        "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
        "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
        "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
        "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"
    ]
    user_agent_set = user_agent_one + user_agent_two + user_agent_three
    user_agent = random.choice(user_agent_two)
    return user_agent
#屏幕分辨率设置
def setDisplay():
    display_size = [
        [1920,1200],
        [1920,1080],
        [1680,1200],
        [1680,1050],
        [1600,900],
        [1440,900],
        [1400,1050],
        [1366,768],
        [1280,960],
        [1280,1024],
        [1080,960],
        [1024,768]
    ]
    d_size = random.choice(display_size)
    dm = win32api.EnumDisplaySettings(None, 0)
    dm.PelsWidth = d_size[0]
    dm.PelsHeight = d_size[1]
    dm.BitsPerPel = 32
    dm.DisplayFixedOutput = 0
    win32api.ChangeDisplaySettings(dm, 0)

def driver_init(search_name):
    try:
        setDisplay()#分辨率
        try:
            s = requests.session()
            s.keep_alive = False
            dailiip = s.get('http://dps.kdlapi.com/api/getdps/?orderid=999617741662079&num=1&pt=1&sep=1')
        except Exception as e:
            print('error-daili')
            return 0 
        options = webdriver.ChromeOptions()
        options.binary_location = r"C:\Chrome\chrome.exe"
        options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
        # options.add_argument('--window-size=1080x960') 百度统计不好使
        # options.add_argument('--headless')
        # 设置代理
        options.add_argument("--proxy-server=%s" % dailiip.text)
    
        # 此步骤很重要，设置为开发者模式，防止被各大网站识别出来使用了Selenium
        options.add_experimental_option('excludeSwitches', ['enable-automation'])
        options.add_experimental_option('useAutomationExtension', False)
        user_ag = get_user_agent()
        options.add_argument('user-agent=%s' % user_ag)
        driver = webdriver.Chrome(r'C:\Chrome\chromedriver.exe',options=options)
        
        # driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        # "source": """
        #     Object.defineProperty(navigator, 'webdriver', {
        #     get: () => undefined
        #     })
        # """
        # })
        driver.maximize_window()
        
        driver.get("https://www.baidu.com")
        time.sleep(random.randint(1, 3))
        driver.implicitly_wait(45)
        searchstr = search_name.split('-')
        for i in searchstr:
            time.sleep(random.randint(1, 3))
            driver.find_element_by_xpath("//*[@id=\"kw\"]").send_keys(i)
            
        # script='''Object.defineProperties(navigator, {webdriver:{get:()=>undefined}})'''
        # driver.execute_script(script)
        randnum1 = random.randint(1, 3)
        time.sleep(randnum1)
        driver.find_element_by_id("su").click()
        time.sleep(3)
        return driver
    except Exception as e:
        print(e)
        # driver_init(search_name)
    


class roll_localpage():
    scrollnum = [200,200,100,300]
    # 在实力初始化的时候初始化参数初始化
    def __init__(self, driver, domane, search_name):
        self.domane = domane
        self.search_name = search_name
        self.driver = driver
    # 实现定位功能 定位到网页后点击
    def find_def(self, page):
        #定位 点击功能
        x = 1
        # 竞价开始
        SEMS = self.driver.find_elements_by_xpath('//*[@id]/div[3]/a/span') #竞价
        for sem in SEMS:
            rd1 = random.randint(1, 6)
            if rd1==3:
               sem.click()
               time.sleep(random.randint(10, 25))
        try:
            fdshouurl = self.driver.find_elements_by_class_name("c-showurl")
            time.sleep(random.randint(3, 5))
            scrolljs = 'window.scrollTo(0, %s)' % (self.scrollnum[random.randint(0, 3)])
            self.driver.execute_script(scrolljs)
            time.sleep(random.randint(3, 5))
        except Exception as e:
            print('no-c-showurl')
            return 0
        for i in fdshouurl:
            id = str((page) * 10 + x)
            if i.text.find(self.domane) != -1:
                time.sleep(1)
                #利用js将定位到的元素拖动到可见区域
                try:
                    bdac = self.driver.find_element_by_xpath("//div[@id=\"" + id + "\"]/h3/a")
                    bdac.click()
                    n_date = datetime.now()
                    n_date = n_date.strftime("%H:%M:%S")
                    print(n_date)
                    print('baidu-success')
                    time.sleep(random.randint(15, 30))
                    # wins = self.driver.window_handles
                    # self.driver.switch_to.window(wins[-1])
                    # catitems = self.driver.find_elements_by_xpath("//ul/li[contains(@class,'cat-item-20')]/a")
                    # catitems_len = len(catitems)-1
                    # random_catitem1 = random.randint(1, catitems_len)
                    # catitems[random_catitem1].click()
                    # time.sleep(random.randint(2, 5))
                    # print('yujianlong.com-success')
                except Exception as e:
                    return 0
                return 0
            else:
                 clickrand = random.randint(10, 40)
                 if clickrand==21:
                     time.sleep(random.randint(3, 8))
                     try:
                        self.driver.find_element_by_xpath("//div[@id=\"" + id + "\"]/h3/a").click()
                        
                        wins = self.driver.window_handles
                        self.driver.switch_to.window(wins[-1])
                        time.sleep(random.randint(10, 15))
                        # self.driver.close()
                        # time.sleep(random.randint(3, 8))
                        self.driver.switch_to.window(wins[0])
                        scrolljs = 'window.scrollTo(0, %s)' % (self.scrollnum[random.randint(0, 3)])
                        self.driver.execute_script(scrolljs)
                        time.sleep(random.randint(3, 5))
                        
                     except Exception as e:
                        print('error-id='+id)
                        print(e)
                        pass
            # 获取当前页面的定位确定处理到那一条数据
            x = x + 1
        return 1

    # 显式超时配置
    def timeouts(self):
        time.sleep(random.randint(3, 5))
        # WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_elements_by_class_name("fk"))

    # 翻页功能
    def roll_page(self, page):
        try:
            for i in range(10):
                self.driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
            time.sleep(random.randint(3, 5))
            for i in self.driver.find_elements_by_class_name("pc"):
                if int(i.text) == int(page + 1):
                    i.click()
                    self.driver.switch_to.window(self.driver.window_handles[0])
                    return 0
            # 翻滚失败返回1
            return 1
        except Exception as e:
            return 1
    def run(self):
        i = 0 
        i1=0
        while i < 6: #从i=0页开始翻页 i表示第几页
            self.timeouts()
            # print(i)
            if 0 == self.find_def(i):
                if i1==1:
                    self.driver.delete_all_cookies()
                    self.driver.quit()
                    break
                time.sleep(random.randint(5, 10))
                self.driver_check()
                i = 0
                i1 += 1
            else:
                i += 1
                self.timeouts()
                self.roll_page(i)
    def driver_check(self):
        try:
            window_len = len(self.driver.window_handles)
            info = self.driver.window_handles
            self.driver.window_handles
            for i in range(window_len  -1 ):
                self.driver.switch_to.window(info[i])
                self.driver.close()
                self.driver.switch_to.window(self.driver.window_handles[0])
            self.driver.get("https://www.baidu.com")
            time.sleep(5)
            self.driver.implicitly_wait(45)
            time.sleep(3)
            searchstr = self.search_name.split('-')
            for i in searchstr:
                time.sleep(random.randint(1, 3))
                self.driver.find_element_by_xpath("//*[@id=\"kw\"]").send_keys(i)
            randnum1 = random.randint(1, 3)
            time.sleep(randnum1)
            self.driver.find_element_by_id("su").click()
            self.timeouts()
        except Exception as driver_error:
                self.driver.quit()
def run_web(check_json):
    driver = driver_init(check_json['search'])
    if driver==0:
        print('error-init')
        return 0
    thread = roll_localpage(driver, check_json['domane'], check_json['search'])
    th1 = threading.Thread(target=thread.run)
    th1.start()
    return 0

a=0
while True:
    a +=1
    print(a)
    keywords = [
        # {"domane": "yujianlong.com", "search": "青岛-网站建设"},
        # {"domane": "yujianlong.com", "search": "青岛网站建设"},
        # {"domane": "yujianlong.com", "search": "青岛网站-建设公司"},
        # {"domane": "yujianlong.com", "search": "青岛-网站建设-公司"},
        {"domane": "yujianlong.com", "search": "青岛-网站-制作公司"},
        {"domane": "yujianlong.com", "search": "青岛-网站制作公司"}
        # {"domane": "yujianlong.com", "search": "青岛-网站制作"},
        # {"domane": "yujianlong.com", "search": "青岛网站-制作"},
        # {"domane": "yujianlong.com", "search": "青岛-做网站"},
        # {"domane": "yujianlong.com", "search": "青岛-做网站"}
        # {"domane": "yujianlong.com", "search": "青岛网站设计"},
        # {"domane": "yujianlong.com", "search": "青岛网站设计"},
        # {"domane": "yujianlong.com", "search": "青岛建站"},
        # {"domane": "yujianlong.com", "search": "青岛建站"},
        # {"domane": "yujianlong.com", "search": "移动网站建设"},
        # {"domane": "yujianlong.com", "search": "移动网站建设"},
        # {"domane": "yujianlong.com", "search": "青岛网站设计"},
        # {"domane": "yujianlong.com", "search": "青岛-网站设计"}
        # {"domane": "yujianlong.com", "search": "青岛建网站公司"},
        # {"domane": "yujianlong.com", "search": "青岛建-网站公司"},
        # {"domane": "yujianlong.com", "search": "青岛网站策划"},
        # {"domane": "yujianlong.com", "search": "青岛网站-策划"},
        # {"domane": "yujianlong.com", "search": "莱芜网站建设"},
        # {"domane": "yujianlong.com", "search": "莱芜-网站建设"},
        # {"domane": "yujianlong.com", "search": "青岛网页设计"},
        # {"domane": "yujianlong.com", "search": "青岛-网页设计"},
    ]
    rand1 = random.randint(0,len(keywords)-1)
    searchsword = keywords[rand1]
  
    run_web(searchsword)
    
    # 日期格式话模版
    format_pattern = "%H:%M:%S"
    end_date = datetime.now()
    end_date = end_date.strftime(format_pattern)
    end_date = datetime.strptime(end_date, format_pattern)
    # 将 'datetime.datetime' 类型时间通过格式化模式转换为 'str' 时间
    if   end_date > datetime.strptime('11:00:00', format_pattern) and end_date < datetime.strptime('13:00:00', format_pattern) :
        time.sleep(random.randint(600, 1000))
    elif end_date > datetime.strptime('17:00:00', format_pattern) and end_date < datetime.strptime('21:00:00', format_pattern) :
        time.sleep(random.randint(1000, 1600))
    elif end_date > datetime.strptime('21:00:00', format_pattern) and end_date < datetime.strptime('23:59:00', format_pattern) :
        time.sleep(random.randint(1500, 2500))
    elif end_date > datetime.strptime('00:00:00', format_pattern) and end_date < datetime.strptime('07:00:00', format_pattern) :
        time.sleep(random.randint(2000, 3600))
    else:
        time.sleep(random.randint(1500, 2500))
